package com.dkd.manage.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.Sku;
import com.dkd.manage.service.ISkuService;
import com.dkd.common.utils.StringUtils;
import com.dkd.common.core.page.TableDataInfo;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.utils.file.FileUtils;
import com.dkd.common.config.RuoYiConfig;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2025-11-10
 */
@RestController
@RequestMapping("/manage/sku")
public class SkuController extends BaseController
{
    @Autowired
    private ISkuService skuService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('manage:sku:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sku sku)
    {
        startPage();
        List<Sku> list = skuService.selectSkuList(sku);
        return getDataTable(list);
    }

    /**
     * 查询启用的商品列表
     */
    @PreAuthorize("@ss.hasPermi('manage:sku:list')")
    @GetMapping("/activeList")
    public AjaxResult activeList(Sku sku)
    {
        List<Sku> list = skuService.selectActiveSkuList(sku);
        return AjaxResult.success(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('manage:sku:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sku sku)
    {
        List<Sku> list = skuService.exportSku(sku);
        ExcelUtil<Sku> util = new ExcelUtil<Sku>(Sku.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 下载商品导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        List<Sku> list = new ArrayList<>();
        // 创建示例数据
        Sku example = new Sku();
        example.setSkuCode("EXAMPLE001");
        example.setSkuName("示例商品");
        example.setSkuClassName("饮料");
        example.setBrand("示例品牌");
        example.setSpec("500ml");
        example.setUnit("瓶");
        example.setPrice(new BigDecimal("5.00"));
        example.setImageUrl("/images/example.jpg");
        example.setStatus(1);
        list.add(example);

        ExcelUtil<Sku> util = new ExcelUtil<Sku>(Sku.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 导入商品数据
     */
    @PreAuthorize("@ss.hasPermi('manage:sku:import')")
    @Log(title = "商品", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        String result = skuService.importSku(file, updateSupport, getUsername());
        return AjaxResult.success(result);
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:sku:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(skuService.selectSkuById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('manage:sku:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sku sku)
    {
        if (StringUtils.isEmpty(sku.getSkuCode()))
        {
            return AjaxResult.error("商品编码不能为空");
        }
        if (StringUtils.isEmpty(sku.getSkuName()))
        {
            return AjaxResult.error("商品名称不能为空");
        }
        if (sku.getSkuClassId() == null)
        {
            return AjaxResult.error("商品分类不能为空");
        }
        if (sku.getPrice() == null || sku.getPrice().compareTo(java.math.BigDecimal.ZERO) <= 0)
        {
            return AjaxResult.error("商品价格必须大于0");
        }

        sku.setCreateBy(getUsername());
        return toAjax(skuService.insertSku(sku));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('manage:sku:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sku sku)
    {
        if (StringUtils.isEmpty(sku.getSkuCode()))
        {
            return AjaxResult.error("商品编码不能为空");
        }
        if (StringUtils.isEmpty(sku.getSkuName()))
        {
            return AjaxResult.error("商品名称不能为空");
        }
        if (sku.getSkuClassId() == null)
        {
            return AjaxResult.error("商品分类不能为空");
        }
        if (sku.getPrice() == null || sku.getPrice().compareTo(java.math.BigDecimal.ZERO) <= 0)
        {
            return AjaxResult.error("商品价格必须大于0");
        }

        sku.setUpdateBy(getUsername());
        return toAjax(skuService.updateSku(sku));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('manage:sku:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(skuService.deleteSkuByIds(ids));
    }
}